package com.pearl.generator.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearl.generator.common.PageResult;
import com.pearl.generator.common.Result;
import com.pearl.generator.pojo.entity.GeneratorEntityConfig;
import com.pearl.generator.pojo.entity.GeneratorMapperConfig;
import com.pearl.generator.pojo.entity.GeneratorPojoConfig;
import com.pearl.generator.pojo.request.ConfigQuery;
import com.pearl.generator.service.PojoConfigService;
import com.pearl.generator.util.GeneratorConfigUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * POJO 配置 前端控制器
 * </p>
 *
 * @author TangDan
 * @since 2022-08-09 11:48:33
 */
@RestController
@RequestMapping("/pojoConfig")
public class PojoConfigController {

    @Resource
    private PojoConfigService pojoConfigService;

    @ApiOperation(value = "分页查询", produces = "application/json")
    @GetMapping("/page")
    public PageResult<List<GeneratorPojoConfig>> page(ConfigQuery configQuery) {
        LambdaQueryWrapper<GeneratorPojoConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(configQuery.getName()), GeneratorPojoConfig::getName, configQuery.getName());
        Page<GeneratorPojoConfig> page = pojoConfigService.page(new Page<>(configQuery.getPageNo(), configQuery.getPageSize()), queryWrapper);
        return PageResult.success(page.getRecords(), page.getTotal());
    }

    @ApiOperation(value = "根据ID删除", produces = "application/json")
    @GetMapping("/remove")
    public Result<?> removeById(@RequestParam Long id) {
        GeneratorConfigUtils.checkDefaultConfigDelete(id);
        pojoConfigService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "新增或修改", produces = "application/json")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody @Validated GeneratorPojoConfig configRequest) {
        pojoConfigService.saveOrUpdate(configRequest);
        return Result.success();
    }

    @ApiOperation(value = "查询所有", produces = "application/json")
    @GetMapping("/list")
    public Result<List<GeneratorPojoConfig>> list() {
        List<GeneratorPojoConfig> list = pojoConfigService.list();
        return Result.success(list);
    }

}
